package com.grass.finding.controller;


import com.google.gson.Gson;
import com.grass.finding.common.BaseResponse;
import com.grass.finding.common.ErrorCode;
import com.grass.finding.exception.BusinessException;
import com.grass.finding.model.domain.User;
import com.grass.finding.service.UserService;
import com.grass.finding.utils.ResultUtil;
import com.grass.finding.utils.WXUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/userInfo")
    public BaseResponse<User> getUserInfo(HttpServletRequest request){
        String openId = WXUtil.getOpenId(request);
        User user = userService.getUserInfoByOpenId(openId);
        return ResultUtil.success(user);
    }

    @GetMapping("/login")
    public BaseResponse<User> login(String code,
                                    String userAvatar,
                                    String userName,
                                    String gender,
                                    HttpServletRequest request){
        // 实现用户登录
        User user = userService.login(code, userAvatar, userName, gender);
        // 将openId存到session中
        request.getSession().setAttribute("openId", user.getOpenId());

        // 用户脱敏化处理
        user.setOpenId("");
        return ResultUtil.success(user);
    }

    @GetMapping("/api/login")
    public BaseResponse<User> testLogin(String openId, HttpServletRequest request){
        // 实现用户登录
        User user = userService.getUserInfoByOpenId(openId);
        // 将openId存到session中
        request.getSession().setAttribute("openId", user.getOpenId());

        // 用户脱敏化处理
        user.setOpenId("");
        return ResultUtil.success(user);
    }

    @PostMapping("/modify/{columnName}/{columnValue}")
    public BaseResponse<String> modifyOneColumnString(@PathVariable String columnName,
                                                 @PathVariable String columnValue,
                                                 HttpServletRequest request){
        // 获取openId
        String openId = (String) request.getSession().getAttribute("openId");
        if(openId == null){
            throw new BusinessException(ErrorCode.NOT_LOGIN);
        }
        // 执行修改操作
        Integer integer = userService.modifyOneColumnString(columnName, columnValue, openId);
        // 返回结果
        return ResultUtil.success(columnValue);
    }
    @PostMapping("/modify/userAvatar")
    public BaseResponse<String> userAvatar(@RequestBody String userAvatar, HttpServletRequest request){
        // 获取openId
        String openId = (String) request.getSession().getAttribute("openId");
        if(openId == null){
            throw new BusinessException(ErrorCode.NOT_LOGIN);
        }
        Map map = new Gson().fromJson(userAvatar, Map.class);
        userAvatar = (String)map.get("userAvatar");
        // 执行修改操作
        Integer integer = userService.modifyOneColumnString("userAvatar",userAvatar, openId);
        // 返回结果
        return ResultUtil.success(userAvatar);
    }
}
